package com.vilinno.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vilinno.tlias.mapper.StudentMapper;
import com.vilinno.tlias.pojo.*;
import com.vilinno.tlias.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


//    /**
//     * 分页查询service曾实现
//     * @param page
//     * @param pageSize
//     * @return
//     */
//    public PageResult<Student> page0(Integer page, Integer pageSize){
//
//        //1. 获取查询结果总记录
//
//        Long total = studentMapper.count();
//
//        //2. 获取查询结果
//
//        Integer start = (page - 1) * pageSize;
//        List<Student> rows=studentMapper.list(start,pageSize);
//
//        //3. 封装结果
//
//        return new PageResult<Student>(total,rows);
//
//    }

    /**
     * 分页查询service曾实现，借助page helper
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam){

        //1. 获取查询结果总记录

        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        //2. 获取查询结果

        List<Student> rows=studentMapper.list(studentQueryParam);
        Page<Student> p=(Page<Student>)rows;

        //3. 封装结果

        return new PageResult<Student>(p.getTotal(),p.getResult());

    }

    /**
     * 新增员工信息
     * 事务注解加在service层中频繁访问数据库操作数据的类、方法、接口（推荐在方法上，颗粒度小）
     * @param student
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void save(Student student){

        //保存员工基本信息
            student.setCreateTime(LocalDateTime.now());
            student.setUpdateTime(LocalDateTime.now());

            studentMapper.insert(student);

    }

    /**
     * 删除员工基本信息的业务层实现
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids){

        // 调用持久层，删除员工基本信息
        studentMapper.deleteByIds(ids);

    }

    @Override
    public Student getInfo(Integer id){

        //1. 调用持久层获取员工基本信息
        return studentMapper.getById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void update(Student student){
        //1. 根据ID修改员工基本信息
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);

    }

    /**
     * 修改同学的违纪情况
     * @param id
     * @param score
     */
    @Override
    public void updateViolation(Integer id, Integer score){
        studentMapper.updateViolation(id,score);
    }
}
